<?php


namespace app\api\controller;

use app\common\model\Member;
use app\common\model\Device as DeviceModel;
use app\validate\DeviceCheck;
use library\Controller;
use think\Db;
use think\Request;
class Device extends Base
{

    //电桩
    public function add(Request $request,DeviceCheck $check,DeviceModel $device){
        try {
            $param = $request->param();
            $result = $check->scene('device_check')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                unset($param['avatar']);
                unset($param['location']);
                unset($param['park_name']);

                $param['remark'] = $param['docs'];
                unset($param['docs']);
                $param['station_id'] = Db::table("store_park")->where(['id'=>$param['park_id']])->value('station_id');
                if(empty($param['station_id'])){
                    return $this->api_result(400, '请设置您的电站！', []);
                }

                $msg = $device->add($param);
                if ($msg==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $msg, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //电桩修改
    public function update(Request $request,DeviceModel $device,DeviceCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('device_save')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                unset($param['avatar']);
                unset($param['location']);
                unset($param['park_name']);

                $param['remark'] = $param['docs'];
                unset($param['docs']);
                $list = $device->device_save($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //电桩列表
    public function index(Request $request,DeviceModel $device,DeviceCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('device_index')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $device->index($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //电桩删除
    public function del(Request $request,DeviceModel $device,DeviceCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('device_del')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $device->del($param);
                if ($list==1) {
                    return $this->api_result(200, '请求成功', []);
                } else {
                    return $this->api_result(400, $list, []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //电桩详情
    public function info(Request $request,DeviceModel $device,DeviceCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('device_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $device->info($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //充电桩收藏
    public function device_collect(Request $request,DeviceModel $device,DeviceCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('device_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $device->device_collect($param);
                return $this->api_result(200, $list, $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //修改充电桩状态
    public function edit_status(){
        $id = input("id")??'';
        $user_id = input("user_id")??'';
        $status = input("status")??'1';
        Db::table("store_device")->where("id='$id'")->where("user_id='$user_id'")->update(['status'=>$status]);
        return $this->api_result(200, '请求成功', []);
    }


    //常用电桩
    public function use_device(){
        $user_id = input("user_id")??'';
        $list = Db::table("store_yinuo_order")->field("device_id")->where(['member_id'=>$user_id])->group("device_id")->select();
        $id_arr = [];
        $date = date('Y-m-d H:i:s');

        foreach ($list as $k =>&$v){
            if(!in_array($v['device_id'],$id_arr)){
                array_push($id_arr,$v['device_id']);
            }
        }
        $arr = Db::table("store_device")->where("id","in",$id_arr)->select();
        foreach ($arr as $k =>&$v){
            $v['flag'] = 0;
            $start_time = date('Y-m-d').' '.$v['start_time'].'00';
            $end_time = date('Y-m-d').' '.$v['end_time'].'00';

            $start_time2 = date('Y-m-d').' '.$v['start_time2'].'00';
            $end_time2 = date('Y-m-d').' '.$v['end_time2'].'00';

            $start_time3 = date('Y-m-d').' '.$v['start_time3'].'00';
            $end_time3 = date('Y-m-d').' '.$v['end_time3'].'00';

            if($v['start_time']&&$v['end_time']){
                if($date>=$start_time){
                    if($date<=$end_time){
                        $v['flag'] = 1;
                    }
                }
            }
            if($v['start_time2']&&$v['end_time2']){
                if($date>=$start_time2){
                    if($date<=$end_time2){
                        $v['flag'] = 1;
                    }
                }
            }
            if($v['start_time3']&&$v['end_time3']){
                if($date>=$start_time3){
                    if($date<=$end_time3){
                        $v['flag'] = 1;
                    }
                }
            }
            $v['use_time'] = Db::table("store_yinuo_order")->where(["device_id"=>$v['id']])->where(['member_id'=>$user_id])->order("id desc")->value("create_at");
        }
        return $this->api_result(200, '请求成功', $arr);
    }

}